package com.sky.service.impl;

import com.sky.context.UserHolder;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    //地址列表
    @Override
    public List<AddressBook> findList(AddressBook addressBook) {
        return addressBookMapper.findList(addressBook);
    }

    //添加地址
    @Override
    public void save(AddressBook addressBook) {
        //1.补齐参数
        addressBook.setUserId(UserHolder.get());
        addressBook.setIsDefault(0);//设置为非默认

        //2.调用mapper保存
        addressBookMapper.save(addressBook);

    }

    //设置默认地址
    @Override
    public void setDefault(AddressBook addressBook) {
        //1.将当前所有的地址设为非默认
        addressBookMapper.updateByUserId(UserHolder.get());

        //2.将指定id地址设置为默认
        addressBook.setIsDefault(1);
        addressBookMapper.update(addressBook);
    }

    //根据id删除地址
    @Override
    public void deleteById(Long id) {
        addressBookMapper.deleteById(id);
    }

    //根据id查询地址
    @Override
    public AddressBook findById(Long id) {
        return addressBookMapper.findById(id);

    }

    //根据id修改地址
    @Override
    public void updateById(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }
}
